package com.xindaoapp.happypet;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.xindaoapp.happypet.activity.FansActivity;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.activity.MenuAttention;
import com.xindaoapp.happypet.activity.MyLoveActivity;
import com.xindaoapp.happypet.activity.MyMessageActivity;
import com.xindaoapp.happypet.activity.MyOrderListActivity;
import com.xindaoapp.happypet.activity.MyPostActivity;
import com.xindaoapp.happypet.activity.MyShopCollectActivity;
import com.xindaoapp.happypet.activity.MyShouCangActivity;
import com.xindaoapp.happypet.activity.SettingActivity;
import com.xindaoapp.happypet.activity.UserInfoActivity;
import com.xindaoapp.happypet.api.ConstantsValue;
import com.xindaoapp.happypet.fragments.ConsigneeFragment;
import com.xindaoapp.happypet.fragments.MessageCenterFragment;
import com.xindaoapp.happypet.utils.BitmapHelp;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.SharePrefUtil;
import com.xindaoapp.happypet.view.CircleImageView;

public class UserCenterFragement extends Fragment implements OnClickListener {
    private RelativeLayout userinfo;
    private RelativeLayout user_tiezi;
    private RelativeLayout user_guanzhu;
    private RelativeLayout user_fans;

    private RelativeLayout my_order_1;
    private RelativeLayout my_order_2;
    private RelativeLayout my_order_3;
    private RelativeLayout my_order_4;

    private RelativeLayout my_order;
    private RelativeLayout my_cats;
    private RelativeLayout my_address;

    private ImageButton cancel;
    private ImageButton publish;

    private CircleImageView user_touxiang;
    private TextView user_name;
    private ImageView iv_tishi;

    private ImageView fans_push;
    private ImageView message_push;


    private SharedPreferences sp;

    private Activity mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressWarnings("static-access")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        mContext = getActivity();
        sp = mContext.getSharedPreferences("userconfig", mContext.MODE_PRIVATE);
        initBitMapUtils();
        initClick();
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_user, null);
        // ViewUtils.inject(view);
        return view;
    }

    private void initClick() {
        userinfo = (RelativeLayout) getView().findViewById(R.id.userinfo);
        user_tiezi = (RelativeLayout) getView().findViewById(R.id.user_tiezi);
        user_guanzhu = (RelativeLayout) getView().findViewById(R.id.user_guanzhu);
        user_fans = (RelativeLayout) getView().findViewById(R.id.user_fans);
        my_order_1 = (RelativeLayout) getView().findViewById(R.id.my_order_1);
        my_order_2 = (RelativeLayout) getView().findViewById(R.id.my_order_2);
        my_order_3 = (RelativeLayout) getView().findViewById(R.id.my_order_3);
        my_order_4 = (RelativeLayout) getView().findViewById(R.id.my_order_4);
        my_order = (RelativeLayout) getView().findViewById(R.id.my_order);
        my_cats = (RelativeLayout) getView().findViewById(R.id.my_cats);
        my_address = (RelativeLayout) getView().findViewById(R.id.my_address);

        user_touxiang = (CircleImageView) getView().findViewById(R.id.user_touxiang);
        user_name = (TextView) getView().findViewById(R.id.user_name);
        iv_tishi = (ImageView) getView().findViewById(R.id.iv_tishi);


        publish = (ImageButton) getView().findViewById(R.id.publish);
        cancel = (ImageButton) getView().findViewById(R.id.cancel);


        fans_push = (ImageView) getView().findViewById(R.id.fans_push);
        message_push = (ImageView) getView().findViewById(R.id.message_push);



        userinfo.setOnClickListener(this);
        user_tiezi.setOnClickListener(this);
        user_guanzhu.setOnClickListener(this);
        user_fans.setOnClickListener(this);

        my_order_1.setOnClickListener(this);
        my_order_2.setOnClickListener(this);
        my_order_3.setOnClickListener(this);
        my_order_4.setOnClickListener(this);

        my_order.setOnClickListener(this);
        my_cats.setOnClickListener(this);
        my_address.setOnClickListener(this);

        publish.setOnClickListener(this);
        cancel.setOnClickListener(this);

        fans_push.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        initUserTouxiangAndName(UserState.isLogged());
        boolean updatenew = SharePrefUtil.getBoolean(mContext, "updatenew", false);
        if (updatenew) {
            iv_tishi.setVisibility(View.VISIBLE);
        } else {
            iv_tishi.setVisibility(View.INVISIBLE);
        }
        super.onResume();
    }

    /**
     * 初始化用户的头像和名字
     */
    private void initUserTouxiangAndName(boolean isLogin) {
        if (isLogin) {
            user_name.setText(UserState.getUser().username);
            if (ConstantsValue.ISUPLOADSUCCESS) {
                if (!TextUtils.isEmpty(sp.getString("uid", ""))) {
                    File filepath = new File(Environment.getExternalStorageDirectory(), "temp");
                    File file = new File(filepath, "userpic.jpg");
                    Bitmap decodeFile = BitmapFactory.decodeFile(file.getAbsolutePath());
                    user_touxiang.setImageBitmap(decodeFile);
                }
            } else {
                if (CommonParameter.isLoadImg) {
                    BitmapHelp.getBitmapUtils(mContext).display(user_touxiang,
                            UserState.getUser().userface, config);
                } else {
                    user_touxiang.setImageResource(R.drawable.unlogin_logo);
                }
            }
        } else {
            user_touxiang.setImageResource(R.drawable.person_loading);
            user_name.setText("");
            // BitmapHelp.getBitmapUtils(mContext).display(user_touxiang,
            // getResources().getDrawable(R.drawable);
        }
    }

    private BitmapDisplayConfig config;

    private BitmapDisplayConfig initBitMapUtils() {
        config = new BitmapDisplayConfig();
        config.setLoadingDrawable(getResources().getDrawable(R.drawable.person_loading));
        config.setLoadFailedDrawable(getResources().getDrawable(R.drawable.person_loading));
        return config;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.publish:
                Intent publish = new Intent(mContext, SettingActivity.class);
                startActivity(publish);
                break;
            case R.id.cancel:
                // finish();
                break;
            case R.id.userinfo:
                if (UserState.isLogged(mContext)) {
                    Intent userinfo = new Intent(mContext, UserInfoActivity.class);
                    startActivity(userinfo);
                }
                break;
            case R.id.user_tiezi:
                if (UserState.isLogged(mContext)) {
                    Intent user_tiezi = new Intent(mContext, MyPostActivity.class);
                    startActivity(user_tiezi);
                }
                break;
            case R.id.user_guanzhu:
                if (UserState.isLogged(mContext)) {
                    Intent user_guanzhu = new Intent(mContext, MenuAttention.class);
                    startActivity(user_guanzhu);
                }
                break;
            case R.id.user_fans:
                if (fans_push.getVisibility() == View.VISIBLE) {
                    fans_push.setVisibility(View.GONE);
                }
                if (UserState.isLogged(mContext)) {
                    Intent user_fans = new Intent(mContext, FansActivity.class);
                    startActivity(user_fans);
                }
                break;
            case R.id.my_order_1:
                if (UserState.isLogged(mContext)) {
                    Intent my_order_1 = new Intent(mContext, MyLoveActivity.class);
                    startActivity(my_order_1);
                }
                break;
            case R.id.my_order_2:
                if (UserState.isLogged(mContext)) {
                    Intent my_order_2 = new Intent(mContext, MyMessageActivity.class);
                    startActivity(my_order_2);
                }
                break;
            case R.id.my_order_3:
                if (UserState.isLogged(mContext)) {
                    Intent my_order_3 = new Intent(mContext, MyShouCangActivity.class);
                    startActivity(my_order_3);
                }
                break;
            case R.id.my_order_4:
                if (UserState.isLogged(mContext)) {
                    Intent my_order_4 = new Intent(mContext, MainActivity.class);
                    my_order_4.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            MessageCenterFragment.class.getCanonicalName());
                    startActivity(my_order_4);
                }
                break;
            case R.id.my_order:
                if (UserState.isLogged(mContext)) {
                    Intent my_order = new Intent(mContext, MyOrderListActivity.class);
                    startActivity(my_order);
                }
                break;
            case R.id.my_cats:
                if (UserState.isLogged(mContext)) {
                    Intent my_cats = new Intent(mContext, MyShopCollectActivity.class);
                    startActivity(my_cats);
                }
                break;
            case R.id.my_address:
                if (UserState.isLogged(mContext)) {
                    Intent my_address = new Intent(mContext, MainActivity.class);
                    my_address.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            ConsigneeFragment.class.getCanonicalName());
                    startActivity(my_address);
                }
                break;
            default:
                break;
        }
    }

}
